package com.timecard.server;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.timecard.client.login.LoginInfo;
import com.timecard.client.login.LoginService;

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public LoginInfo login(String requestUri) {
		LoginInfo loginInfo = new LoginInfo();
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		if(user != null) {
			loginInfo.setLoogedIn(true);

			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
			loginInfo.setNickname(user.getNickname());
			loginInfo.setEmailAddress(user.getEmail());
		} else {
			loginInfo.setLoogedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}
		return loginInfo;
	}

}
